﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">
    <title>易得桌面</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageStyles" runat="server">
    <style type="text/css">
        .message
        {
            clear: both;
            margin-bottom: 10px;
            padding: 5px;
            height: 120px;
        }
        div.message div.avatar
        {
            float: left;
            width: 55px;
        }
        div.message div.meta
        {
            color: #666666;
            font-size: 12px;
            margin: 5px 0;
        }
        
        div.message h1 a.needConfirm
        {
            color: red;
            text-decoration: none;
            font-size: 18px;
        }
        
        div.message h1 a.notNeedConfirm
        {
            color: #000000;
            text-decoration: none;
            font-size: 18px;
        }
        div.message div.content
        {
            margin-left: 65px;
        }
        div.formatted_text_body.rich_text
        {
            margin-bottom: 10px;
        }
        
        div.formatted_text_body
        {
            font-size: 13px;
            line-height: 1.425em;
        }
    </style>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="PageScripts" runat="server">
    <script src="../../Scripts/jquery.quickpaginate.js" type="text/javascript"></script>
    <script type="text/javascript">
    //************* URL常量
    var URL_MESSAGE_SHOWMESSAGE = "";
    var URL_MESSAGE_INBOX = "";
    //************* 全局变量
    var g_Var = {
        "messageTable": null

    };
    //************* 常量定义
    var StaticResource = {
        "hasConfirmed": {
            "true": "我已确认",
            "false": "还未确认"
        }
    };

    //************* Accessor访问器
    var EzAccessor = {};

    if (ezmock.IS_MOCKED) {
        var mock_json =
	        [{
	            "MessageId": "33333",
	            "NeedConfirm": true,
	            "HasConfirmed": true,
	            "SendUser": {
	                "Id": "33333",
	                "Name": "加菲盐",
	                "UserName": "yuxin",
	                "Dept":
                        {
                            "Id": "bd3474a8-6c2d-43ef-bf42-b06827a216f6",
                            "DeptName": "软件工程",
                            "GroupId": "9ad96dac-88d8-4315-bfdb-9931c8f3cb7c"
                        }

	            },
	            "CreateOn": "1348423559",
	            "MessageTitle": "",
	            "MemberString": null,
	            "MessageText": '8月的尾巴了。2010年3月，美丽开始做DOTA的解说，4月，慢慢进入大家的视线。'
	        },
            {
                "MessageId": "44444",
                "NeedConfirm": false,
                "HasConfirmed": false,
                "SendUser": {
                    "Id": "33333",
                    "Name": "何静",
                    "UserName": "yuxin",
                    "Dept":
                        {
                            "Id": "bd3474a8-6c2d-43ef-bf42-b06827a216f6",
                            "DeptName": "软件工程",
                            "GroupId": "9ad96dac-88d8-4315-bfdb-9931c8f3cb7c"
                        }

                },
                "CreateOn": "1348423559",
                "MessageTitle": "发牢骚",
                "MemberString": null,
                "MessageText": '普不普通话一点都不重要~有风格就行了，口音也算是一种风格了。'
            },
            {
                "MessageId": "55555",
                "NeedConfirm": true,
                "HasConfirmed": false,
                "SendUser": {
                    "Id": "33333",
                    "Name": "张璐",
                    "UserName": "yuxin",
                    "Dept":
                        {
                            "Id": "bd3474a8-6c2d-43ef-bf42-b06827a216f6",
                            "DeptName": "软件工程",
                            "GroupId": "9ad96dac-88d8-4315-bfdb-9931c8f3cb7c"
                        }

                },
                "CreateOn": "1348423559",
                "MessageTitle": "",
                "MemberString": null,
                "MessageText": '支持。口音也算是一种风格了'
            }, {
                "MessageId": "55555",
                "NeedConfirm": false,
                "HasConfirmed": false,
                "SendUser": {
                    "Id": "33333",
                    "Name": "张璐",
                    "UserName": "yuxin",
                    "Dept":
                        {
                            "Id": "bd3474a8-6c2d-43ef-bf42-b06827a216f6",
                            "DeptName": "软件工程",
                            "GroupId": "9ad96dac-88d8-4315-bfdb-9931c8f3cb7c"
                        }

                },
                "CreateOn": "1348423559",
                "MessageTitle": "支持",
                "MemberString": null,
                "MessageText": '支持。口音也算是一种风格了'
            }, {
                "MessageId": "55555",
                "NeedConfirm": false,
                "HasConfirmed": false,
                "SendUser": {
                    "Id": "33333",
                    "Name": "张璐",
                    "UserName": "yuxin",
                    "Dept":
                        {
                            "Id": "bd3474a8-6c2d-43ef-bf42-b06827a216f6",
                            "DeptName": "软件工程",
                            "GroupId": "9ad96dac-88d8-4315-bfdb-9931c8f3cb7c"
                        }

                },
                "CreateOn": "1348423559",
                "MessageTitle": "",
                "MemberString": null,
                "MessageText": '支持。口音也算是一种风格了'
            }];

        var mockEzAccessor = {
            'getMessageList': function () {
                return mock_json;
            },
            'getCurrentUser': function () {

            },
            'doShowMessage': function (messageId) {
                alert("查看消息" + messageId);
            }
        };
        EzAccessor = mockEzAccessor;
    } else {
        var realEzAccessor = {
            'getMessageList': function () {
                return  <%= ViewData["Inbox"] %>;
            },
            'getCurrentUser': function () {
                return <%= ViewData["CurrentUser"] %>;
            },
            'doShowMessage': function (messageId) {
                window.location.href = "show?TopicId=" + messageId;
            }
        };
        EzAccessor = realEzAccessor;
    }



    function quickPage() {
        $("#cmbInBox .message").quickpaginate({ perpage: 10, showcounter: true, pager: $("#pageCount") });
    }

    function bindInboxMessageList(messageList) {
        if (messageList.length != 0) {
            var messageInboxDom = $('<div id="cmbInBox"></div>');

            for (var key in messageList) {
                //发送者头像
                //todo:
                //获取发送者头像
                var avatarStr = '<div class="avatar"><img src="/core/headicon/{userId}" style="width:54px;heigth:54px;"></img></div>';
                avatarStr = avatarStr.replace("{userId}", messageList[key].Topic.Sender.Id);

                //发送者发送时间
                var senderName = messageList[key].Topic.Sender.Name;
                var deptName = messageList[key].Topic.Sender.Dept.DeptName;
                var time = Date.fromEpoch(messageList[key].Topic.CreateOn);
                var createOn = time.toNiceTime();

                var senderStr = '<div class="meta"><a>{senderName}</a> ({deptName}) <span style="float:right;">{createOn}</span></div>';
                senderStr = senderStr.replace("{senderName}", senderName)
                                 .replace("{deptName}", deptName)
                                 .replace("{createOn}", createOn);
                //消息参与者
                var  partnerStr = '<p>参与者:  {groupInfo}  {userInfo}</p>';
                
                var gorupInfo = messageList[key].Topic.Participator.GroupParticipators;
                var userInfo = messageList[key].Topic.Participator.UserParticipators; 

                if(gorupInfo.length != 0 ){
                    for (var group in gorupInfo){
                        partnerStr = partnerStr.replace("{groupInfo}", gorupInfo[group].Description);     
                    }
                }
                else{
                    partnerStr = partnerStr.replace("{groupInfo}", " ");   
                }


                if(userInfo.length != 0 ){ 
                    var nameList = "";
                    var number  =  0;
                    for(var user in userInfo){
                        
                        if (user < 5) {
                            nameList = nameList + userInfo[user].Name + " ";
                        }
                        else {
                            number = user;
                        }
                    }
                    if (number >= 5) {
                            nameList = nameList + "等";
                        }
                    partnerStr = partnerStr.replace("{userInfo}", nameList);
                }
                else{
                    partnerStr = partnerStr.replace("{userInfo}", " "); 
                }
                    
                //消息标题s
                var title = messageList[key].Topic.MessageTitle;
                var titleStr = null;
                if (title.length != 0) {
                    titleStr = '<span class="msgTitle" style="font-weight:bold;font-size:16px;">{title}</span>&nbsp;';
                    titleStr = titleStr.replace("{title}", title);
                }

                

                //消息内容
                var content = messageList[key].Topic.MessageText;
                var contentStr = '<div class="formatted_text_body"><div>{content}</div></div>';
                contentStr = contentStr.replace("{content}", content);

                var titleAndConfirmDom = $('<div></div>')
                var contentDom = $('<div class="content"></div>');
                var messageDom = $('<div class="message"></div>');
                var lableDom = $('<div class="msgLbl"></div>');
                //是否需要确认
                var isNeedConfirm = messageList[key].Topic.NeedConfirm;
                //是否已经确认
                var hasConfirmedStr = null;
                if (isNeedConfirm == true) {
                    var hasConfirmed = messageList[key].HasConfirmed;
                    hasConfirmedStr = '<p style="font-size:14px; color:{color}; font-weight:bold;">({hasConfirmed})</p>';
                    if (hasConfirmed == true) {
                        hasConfirmedStr = hasConfirmedStr.replace("{hasConfirmed}", StaticResource.hasConfirmed['true'])
                                                         .replace("{color}", "#090");
                    } else {
                        hasConfirmedStr = hasConfirmedStr.replace("{hasConfirmed}", StaticResource.hasConfirmed['false'])
                                                         .replace("{color}", "red");
                    }
                }
                
                $(titleAndConfirmDom).append(titleStr);
                $(contentDom).append(senderStr)
                             .append(partnerStr)
                             .append(titleAndConfirmDom)
                             .append(contentStr)
                              .append(hasConfirmedStr);
                
                $(messageDom).append(avatarStr)
                             .append(contentDom);
                            
                
                $(lableDom).append(messageDom);
                $(lableDom).data("messageId", messageList[key].Topic.MessageId);
                
                $(messageInboxDom).append(lableDom);
               
                //定义action
                lableDom.click(function() {
                    var messageId = $(this).data("messageId");
                    EzAccessor.doShowMessage(messageId);
                });

                lableDom.hover(function() {
                    $(this).css({
                        "background-color": "#eee",
                        "cursor" : "pointer",
                        "-webkit-border-radius" : "8px"
                    });
                });
                lableDom.mouseleave(function(){
                    $(this).css({
                        "background-color": "#fff"   
                    });    
                });

            }

            $('#messageInbox').append(messageInboxDom);
        }
        else {
            var noMessageDom = '<div style="text-align:center">没有消息</div>';
            $('#messageInbox').append(noMessageDom);
        }

    }
    function deleMessage(id) {
        //增添tips
        var deletInfo = '<p>你确定要删除这条消息吗？</p><input type="button" id="delete" value="删除"/><input type="button" id="cancel" value="取消"/>';

        //去除已有的tips
        $('.tip-yellow').remove();

        $('#' + id).poshytip({
            content: deletInfo,
            showOn: 'focus',
            alignTo: 'target',
            alignX: 'left',
            alignY: 'center',
            offsetX: 5
        });

        $('#' + id).poshytip('show');

        //取消删除
        $('#cancel').click(function () {
            $('.tip-yellow').remove();

        });

        //确定删除
        $('#delete').click(function () {
            $('.tip-yellow').remove();

            var messageTr = $('#' + id).parent().parent();
            var position = g_Var['messageTable'].fnGetPosition(messageTr[0]);
            g_Var['messageTable'].fnDeleteRow(position);

        })

    }

    function initPage() {
        EzTipsManager.addTipBox('#tip_Remind', 'tipRemind');
        EzTipsManager.addTipBox('#tip_outbox', 'tipOutBox');
        EzTipsManager.showTipBox('tipRemind');
        EzTipsManager.showTipBox('tipOutBox');

        var messageList = EzAccessor.getMessageList();
        bindInboxMessageList(messageList);
        quickPage();
    }

    function initPageEvent() {
        $('#outbox').click(function () {
            window.location.href = "/message/outbox";
        });

    }

    $(function () {
        initPage();
        initPageEvent();
    });
    </script>
</asp:Content>

<asp:Content ID="Content4" ContentPlaceHolderID="PageHead" runat="server">
    <% Html.RenderPartial("_sayHello"); %>
    <div id="status-bar"><% Html.RenderPartial("_navigateTo"); %></div>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="PageContent" runat="server">
    <div id="page-head">
        <img src="../../Content/icons/message_inbox.png" style="width: 48px; height: 48px;"
            alt="outbox" />
        <span>我的收件箱</span>
    </div>
    <div id="page-body">
        <div id="messageInbox"></div>
    </div>
    <div class="page-submit">
        <div id="pageCount">
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="PageShortcut" runat="server">
    <div id="shortcuts" class="box">
        <a class="btn-shortcut" href="/message/send">写新消息</a>
    </div>
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="PageFoot" runat="server">
    <div id="tip_Remind" style="display: none;">
        <h1>
            小贴士:</h1>
        <p class="indent">
            标题为红色的消息，说明该消息的发送者希望您查阅过该消息后，予以确认。请及时查阅并确认您需要确认的消息！
        </p>
    </div>
    <div id="tip_outbox" style="display: none;">
        <h1>
            <img src="../../Content/icons/message_outbox.png" style="width: 48px; height: 48px;"
                alt="inbox" /><a id="outbox">我的发件箱</a>
        </h1>
    </div>
</asp:Content>
